package hai.com.android_test.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.lidroid.xutils.exception.DbException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import hai.com.android_test.Bean.User;
import hai.com.android_test.R;
import hai.com.android_test.utils.Helper;

/**
 * Created by 黄海 on 11/27/2016.
 */

public class DbUtilActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG ="DbUtilActivity" ;
    @BindView(R.id.button)
    Button btn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xutil);
        ButterKnife.bind(this);
    }

    @Override
    @OnClick({R.id.button})
    public void onClick(View v) {
        insert();

        System.out.println(getUser().toString());
    }

    User getUser() {
        try {
//            return Helper.dbUtils.findAll(User.class).get(0);
            return Helper.dbUtils.findById(User.class,1);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    private void insert() {
        User user = new User(1, "hai", "123");
        try {
            Helper.dbUtils.save(user);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
}
